/*
 * JSON encapsulate module.
 */
#ifndef _JSON_H_
#define _JSON_H_

#include "list.h"

#define INIT_BUF_SIZE 		(32*1024)
#define MAX_LIST_COUNT 	20
#define MAX_STR_LEN 512

typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedef unsigned long long uint64;

struct META;

typedef enum 
{
	RT_CONFIG, 
	RT_GETPARAM
} request_type_e ;// request type for config and getparameter, result of parse

typedef enum stbDataType
{
	STB_DATA_UNKNOW = 0,
	STB_DATA_UINT8,
	STB_DATA_UINT16,
	STB_DATA_UINT32,
	STB_DATA_UINT64,
	STB_DATA_TIMEVAL,
	STB_DATA_ENUM,
	STB_DATA_STR,
	STB_DATA_STR_PTR,
	STB_DATA_BOOL,
	STB_DATA_FLOAT,
	STB_DATA_UINT8_ARRAY,  // seperate by ,
	STB_DATA_UINT32_ARRAY,
	STB_DATA_IPADDR,  // ip address type
	JSON_DATA_LIST,
	JSON_DATA_PTR,

	STB_DATA_BEAN,
	STB_DATA_BEAN_END,
	STB_DATA_LIST,
	STB_DATA_LIST_END,

	STB_DATA_NULL,		//don't add
	STB_DATA_BEAN_NULL, 	//NULL BEAN

	STB_DATA_LAST
} stbDataType;

typedef struct jason_parse_resule
{
	request_type_e type;  // 
	void *data;	// data depends on the request type 
} json_parse_result_t;

typedef enum 
{
	OR_SUCCESS, 
	OR_FAILED, 
	OR_
} operator_return_e;

typedef enum 
{
	OP_RESP, 
	OP_RR, 
	OP_POLL, 
	OP_HTTPCLIENT_SENDRESP, 
	OP_HTTPCLIENT_RCV
	
} operator_e; 

typedef enum parseRet
{
	PR_SUCCESS,
	PR_PARA_NULL,
	PR_STR_OVERFLOW,
	PR_FORMAT_ERROR,
	PR_COM_UNKNOW,
	PR_INVALID_OPT,
	PR_NOT_SUPPORT
	
}parseRet;

typedef enum encapsulateRet
{
	ER_SUCCESS,
	ER_PARA_NULL,
	ER_NOT_SUPPORT
	
}encapsulateRet;

typedef struct jsonPara
{
	char name[30];
	stbDataType type;		//data type
	char value[MAX_STR_LEN];
	
}jsonPara;

DEF_LIST_WITH_TYPE(jsonPara, uint32, uint32);

// Encapsulate json structure
typedef struct jsonStruct
{
	jsonPara_list list;
	operator_e opt;
	char* buf;
	int buf_size;
}jsonStruct;

static int insertPrarmeterList(jsonPara_list * list, char* name, stbDataType type, char* value);

int setRequestType(jsonStruct* message, operator_e opt);
char* getJsonEncapsulateStr(jsonStruct* message);
int  format(jsonStruct *message);
int freeJson(jsonStruct *message);

/*
 * parse the request string from the qcs
 * data - string buffer
 * length - the length of data string
 */
parseRet  qcsRequestParse(operator_e opt, char *data, int length);

/*
 * encapsulate the parameters in a struct to the list or strings.
 * message - the destinations for the encapsulate struct.
 * name - the name string of the type 
 * data - the point to the struct, which will be encased.
 * meta - the point to the meta data of struct 
 */
encapsulateRet encapsulateJsonList(jsonStruct* message, char* name, void* data, struct META* meta, uint16 type);

#endif // _JSON_H_

